package com.ruoyi.hotel.controller;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.hotel.domain.HmRoom;
import com.ruoyi.hotel.domain.HmRoomType;
import com.ruoyi.hotel.domain.bo.HmRoomBo;
import com.ruoyi.hotel.mapper.HmRoomMapper;
import com.ruoyi.hotel.mapper.HmRoomTypeMapper;
import com.ruoyi.hotel.service.IHmRoomTypeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.domain.vo.HmRoomVo;
import com.ruoyi.hotel.service.IHmRoomService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间管理
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/room")
public class HmRoomController extends BaseController {

    private final IHmRoomTypeService iHmRoomTypeService;
    private final IHmRoomService iHmRoomService;

    /**
     * 查询房间列表
     */
    @SaCheckRole("hotel:room:list")
    @GetMapping("/list")
    public TableDataInfo<HmRoomVo> list(HmRoomBo bo, PageQuery pageQuery) {
        return iHmRoomService.queryPageList(bo, pageQuery);
    }

    /**
     * 修改房间
     */
    @SaCheckRole("logistics")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateRoom")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmRoomBo bo) {
        return toAjax(iHmRoomService.updateByBo(bo));
    }


    /**
     * 修改房间状态
     * @param room
     */
    @SaCheckRole("logistics")
    @RequestMapping("/setRoom")
    public R<Void> setRoom(@RequestBody HmRoom room){

       Boolean flag=iHmRoomService.setRoom(room);
       //false说明时occupied的
       if(flag){
           //work更改后为free
           if(room.getRoomStatus().equals("f")){
               iHmRoomTypeService.addRoomCount(room);
           }
           //free更改后为work
           if(room.getRoomStatus().equals("w")){
               iHmRoomTypeService.minusRoomCount(room);
           }
           //occupied在bill时改为free，此处应该可以忽略
           return toAjax(true);
       }
       else{
           return toAjax(false);
       }

    }

    /**
     * 新增房间
     */
    @SaCheckRole("logistics")
    @SaCheckPermission("hotel:room:add")
    @Log(title = "添加房间", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addRoom")
    public void add(@RequestBody HmRoom room) {
        room.setRoomStatus("f");

        iHmRoomService.insertById(room);
        iHmRoomTypeService.addRoomCount(room);

    }




}
